/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.st.entity.StUser;
import com.jeesite.modules.st.dao.StUserDao;
import com.jeesite.modules.st.entity.StPhone;
import com.jeesite.modules.st.dao.StPhoneDao;

/**
 * 索韬用户表Service
 * @author HOHD
 * @version 2020-12-09
 */
@Service
@Transactional(readOnly=true)
public class StUserService extends CrudService<StUserDao, StUser> {
	
	@Autowired
	private StPhoneDao stPhoneDao;
	
	/**
	 * 获取单条数据
	 * @param stUser
	 * @return
	 */
	@Override
	public StUser get(StUser stUser) {
		StUser entity = super.get(stUser);
		if (entity != null){
			StPhone stPhone = new StPhone(entity);
			stPhone.setStatus(StPhone.STATUS_NORMAL);
			entity.setStPhoneList(stPhoneDao.findList(stPhone));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param stUser 查询条件
	 * @param stUser.page 分页对象
	 * @return
	 */
	@Override
	public Page<StUser> findPage(StUser stUser) {
		return super.findPage(stUser);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param stUser
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(StUser stUser) {
		super.save(stUser);
		// 保存 StUser子表
		for (StPhone stPhone : stUser.getStPhoneList()){
			if (!StPhone.STATUS_DELETE.equals(stPhone.getStatus())){
				stPhone.setUserId(stUser);
				if (stPhone.getIsNewRecord()){
					stPhoneDao.insert(stPhone);
				}else{
					stPhoneDao.update(stPhone);
				}
			}else{
				stPhoneDao.delete(stPhone);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param stUser
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(StUser stUser) {
		super.updateStatus(stUser);
	}
	
	/**
	 * 删除数据
	 * @param stUser
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(StUser stUser) {
		super.delete(stUser);
		StPhone stPhone = new StPhone();
		stPhone.setUserId(stUser);
		stPhoneDao.deleteByEntity(stPhone);
	}
	
}